'use strict';

angular.module('hanxinBroker').directive('checkIsDiff', function twIsWord() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attrs, ngModel) {
      ngModel.$parsers.push(function (value) {
        if (_.isUndefined(value)) {
          return undefined;
        }

        var anotherValue = scope.$eval(attrs.checkIsDiff);
        var isDiff = value !== anotherValue;
        ngModel.$setValidity('diff', isDiff);

        return isDiff ? value : undefined;
      });
      scope.$watch(
        function () {
          return scope.$eval(attrs.checkIsDiff);
        },
        function (anotherValue) {
          var isDiff = ngModel.$modelValue !== anotherValue;
          ngModel.$setValidity('same', isDiff);
        }
      );
    }
  };
});
